package com.jy.appmarket.activity;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.HashMap;

import com.jy.appmarket.R;
import com.jy.appmarket.utils.NetTools;
import com.umeng.analytics.MobclickAgent;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class PermissionListActivity extends Activity implements OnItemClickListener{
	public static final String EXTRA_PERMISSIONS = "permissions";
	private String mPermissions[];
	private HashMap<String, String> mPermissionDetail;
	private ListView mListView;
	private TextView mTextView;
	private int mClickCount = 0;

	private HashMap<String, String> getPermissionDetail(Context context) {
		HashMap<String, String> permissiondetail = new HashMap<String, String>();
		AssetManager am = context.getAssets();
		InputStream fis = null;
		try {
			fis = am.open("permissontable.txt");
			Reader reader = null;
			BufferedReader br = null;
			try {
				reader = new InputStreamReader(fis);
				br = new BufferedReader(reader);
				String key = null;
				String detail = null;
				while ((key = br.readLine()) != null) {
					detail = br.readLine();
					if (key.length() != 0) {
						if (detail != null && detail.length() > 0) {
							permissiondetail.put(key, detail);
						} else {
							permissiondetail.put(key, "");
						}
					}
				}
			} catch (Exception e) {
				return null;
			} finally {
				try {
					if (br != null) {
						br.close();
					}
					if (reader != null) {
						reader.close();
					}
				} catch (Exception e) {
					// Ignore anything
				}
			}
		} catch (Exception e) {
			return null;
		}
		return permissiondetail;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (getIntent() != null) {
			mPermissions = getIntent().getStringArrayExtra(EXTRA_PERMISSIONS);
			mPermissionDetail = getPermissionDetail(getApplicationContext());
		}
		setContentView(R.layout.activity_permissions);
		ActionBar actionBar = getActionBar();
		actionBar.setTitle(R.string.app_permission);
		actionBar.setDisplayUseLogoEnabled(false);
		actionBar.setDisplayShowHomeEnabled(false);
		actionBar.setDisplayHomeAsUpEnabled(true);
		mListView = findViewById(R.id.permissions_listview);
		mListView.setOnItemClickListener(this);
		mTextView = findViewById(R.id.permissions_empty);
		if (mPermissions == null || mPermissions.length == 0) {
			mListView.setVisibility(View.GONE);
			mTextView.setVisibility(View.VISIBLE);
		} else {
			PermissionAdapter adapter = new PermissionAdapter();
			mListView.setAdapter(adapter);
			mListView.setVisibility(View.VISIBLE);
			mTextView.setVisibility(View.GONE);
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}

	private class PermissionAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return mPermissions.length;
		}

		@Override
		public Object getItem(int position) {
			return mPermissions[position];
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Holder holder = null;
			if (convertView == null) {
				convertView = LayoutInflater.from(PermissionListActivity.this).inflate(R.layout.permission_item, parent,
						false);

				holder = new Holder();
				holder.text = convertView.findViewById(R.id.permission_item_text);
				holder.detail = convertView.findViewById(R.id.permission_item_detail);
				convertView.setTag(holder);
			} else {
				holder = (Holder) convertView.getTag();
			}
			holder.text.setText(mPermissions[position]);
			if (mPermissionDetail != null) {
				String detail = mPermissionDetail.get(mPermissions[position]);
				if (detail != null && detail.length() > 0) {
					holder.detail.setText(detail);
				} else {
					holder.detail.setText(mPermissions[position]);
				}
			} else {
				holder.detail.setText(mPermissions[position]);
			}
			return convertView;
		}
	}

	private static class Holder {
		TextView text;
		TextView detail;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		switch (position) {
		case 0:
			++mClickCount;
			if (mClickCount > 4) {
				String versionName = NetTools.getVersionName(this);
				String channel = NetTools.getChannelNumber(this);
				String text = "版本号：" + versionName + "\n渠道号：" + channel;
				Toast.makeText(this, text, Toast.LENGTH_LONG).show();
				mClickCount = 0;
			}
			break;

		default:
			break;
		}
	}

}
